﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Bootstrap5.Abstractions;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;

namespace Bootstrap5;

/// <summary>
/// An input component for editing range values. Supported range types are <see cref="System.Int32"/>,<see cref="System.Int64"/>, <see cref="System.Int16"/>,<see cref="System.Single"/> ,<see cref="System.Double"/> ,<see cref="System.Decimal"/> .
/// </summary>
/// <typeparam name="TValue">The type that must support <see cref="System.Int32"/>,<see cref="System.Int64"/>, <see cref="System.Int16"/>,<see cref="System.Single"/> ,<see cref="System.Double"/> ,<see cref="System.Decimal"/> .</typeparam>
[CssClass("form-range")]
public class FormInputRange<TValue>:FormInputBase<TValue>
{
	static FormInputRange()
	{
		Type type = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue);
		if (type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(float) || type == typeof(double) || type == typeof(decimal))
		{
			return;
		}

		throw new InvalidOperationException($"The type '{type}' is not a supported numeric type.");
	}

	protected override string Type => "range";

	[Parameter] [HtmlProperty("min")] public decimal? Min { get; set; }
	[Parameter] [HtmlProperty("max")] public double? Max { get; set; }
	[Parameter] [HtmlProperty("step")] public double? Step { get; set; }

	protected override void OnParametersSet()
	{
		if(Min.HasValue && Min.Value < 0)
		{
			throw new InvalidOperationException($"The value of {nameof(Min)} must not be less than 0");
		}
		if(Max.HasValue && Max.Value > 100)
		{
			throw new InvalidOperationException($"The value of {nameof(Max)} must mot be grater than 100");
		}
	}
}
